package com.simple.piger.service.impl;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.simple.piger.constant.AjaxCode;
import com.simple.piger.dto.req.InviteRecordReqDto;
import com.simple.piger.dto.resp.InviteRecordRespDto;
import com.simple.piger.dto.resp.company.InviteCompanyEmpRespDto;
import com.simple.piger.entity.ApplyUser;
import com.simple.piger.entity.InviteCompanyEmp;
import com.simple.piger.entity.InviteCompanyJob;
import com.simple.piger.entity.InviteRecord;
import com.simple.piger.enums.ApplyUserEduEnums;
import com.simple.piger.enums.GenderEnums;
import com.simple.piger.enums.InviteRecordStatusEnums;
import com.simple.piger.mapper.InviteRecordMapper;
import com.simple.piger.service.*;
import com.simple.piger.utils.AjaxResult;
import com.simple.piger.utils.DateUtils;
import com.simple.piger.utils.IdUtil;
import com.simple.piger.utils.PageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 求职记录 服务实现类
 * </p>
 *
 * @author baolw
 * @since 2020-02-06
 */
@Service
public class InviteRecordServiceImpl extends ServiceImpl<InviteRecordMapper, InviteRecord> implements InviteRecordService {
    @Autowired
    private ApplyUserService applyUserService;
    @Autowired
    private InviteCompanyJobService inviteCompanyJobService;
    @Autowired
    private InviteCompanyEmpService inviteCompanyEmpService;
    @Autowired
    private JobTitleService jobTitleService;

    @Override
    public AjaxResult invite(InviteRecordReqDto reqDto) {
        EntityWrapper<InviteRecord> inviteRecordEw = new EntityWrapper<>();
        inviteRecordEw.eq("apply_user_id", reqDto.getApplyUserId());
        inviteRecordEw.eq("invite_company_job_id", reqDto.getInviteCompanyJobId());
        inviteRecordEw.lt("create_time", LocalDate.now().plusDays(3));
        if (this.selectCount(inviteRecordEw) > 0) {
            return new AjaxResult<>(AjaxCode.BUSINESS_INFO, "3天内不能重复投递同一岗位");
        }

        EntityWrapper<ApplyUser> auEw = new EntityWrapper<>();
        auEw.eq("cms_sys_user_id", reqDto.getCmsSysUserId());
        ApplyUser applyUser = applyUserService.selectOne(auEw);

        InviteRecord record = new InviteRecord();
        record.setApplyUserId(applyUser.getId());
        record.setId(IdUtil.getId());
        record.setInviteCompanyJobId(reqDto.getInviteCompanyJobId());
        record.setCreateTime(new Date());
        record.setDeleteFlag("0");
        record.setInviteCompanyJobId(record.getInviteCompanyJobId());
        record.setUpdateTime(new Date());
        record.setJobRecordFileUrl(applyUser.getJobRecordFileUrl());
        record.setStatus(InviteRecordStatusEnums.$2.getItem());
        record.setCompanyId(reqDto.getCompanyId());
        record.insert();
        return new AjaxResult(200, null);
    }

    @Override
    public List<InviteRecordRespDto> list(Integer page, Integer limit, String cmsSysUserId) {
        EntityWrapper<ApplyUser> ew = new EntityWrapper<>();
        ew.eq("cms_sys_user_id", cmsSysUserId);
        ApplyUser applyUser = applyUserService.selectOne(ew);

        List<InviteRecordRespDto> list = this.baseMapper.list(PageUtil.getPageStart(page, limit), limit
                , null, applyUser.getId());
        for (InviteRecordRespDto respDto : list) {
            respDto.setJobTitleValue(jobTitleService.selectById(respDto.getJobTitle()).getName());
            respDto.setStatusName(InviteRecordStatusEnums.transfer(respDto.getStatus()));
            respDto.setInviteDateStr(DateUtils.dateFormat(respDto.getInviteDate(), "yyyy-MM-dd HH:mm:ss"));
        }
        return list;
    }

    @Override
    public int count(String cmsSysUserId) {
        EntityWrapper<ApplyUser> ew = new EntityWrapper<>();
        ew.eq("cms_sys_user_id", cmsSysUserId);
        ApplyUser applyUser = applyUserService.selectOne(ew);

        EntityWrapper<InviteCompanyEmp> empEw = new EntityWrapper<>();
        empEw.eq("apply_user_id", applyUser.getId());
        InviteCompanyEmp emp = inviteCompanyEmpService.selectOne(empEw);
        return this.baseMapper.count(emp.getInviteCompanyId(), applyUser.getId());
    }

    @Override
    public List<InviteRecordRespDto> companyList(Integer page, Integer limit, String cmsSysUserId, String userId) {
        EntityWrapper<ApplyUser> ew = new EntityWrapper<>();
        ew.eq("cms_sys_user_id", cmsSysUserId);
        ApplyUser applyUser = applyUserService.selectOne(ew);

        EntityWrapper<InviteCompanyEmp> empEw = new EntityWrapper<>();
        empEw.eq("apply_user_id", applyUser.getId());
        InviteCompanyEmp emp = inviteCompanyEmpService.selectOne(empEw);

        List<InviteRecordRespDto> list = this.baseMapper.list(PageUtil.getPageStart(page, limit), limit, emp.getInviteCompanyId()
                ,null);
        for (InviteRecordRespDto respDto : list) {
            respDto.setStatusName(InviteRecordStatusEnums.transfer(respDto.getStatus()));
            respDto.setGenderValue(GenderEnums.transfer(respDto.getGender()));
            respDto.setEduBackgroundValue(ApplyUserEduEnums.transfer(respDto.getEduBackground()));
            respDto.setJobTitleValue(jobTitleService.selectById(respDto.getJobTitle()).getName());
            respDto.setInviteDateStr(DateUtils.dateFormat(respDto.getInviteDate(), "yyyy-MM-dd HH:mm:ss"));
        }
        return list;
    }

    @Override
    public int companyListCount(String cmsSysUserId, String companyId) {
        EntityWrapper<ApplyUser> ew = new EntityWrapper<>();
        ew.eq("cms_sys_user_id", cmsSysUserId);
        ApplyUser applyUser = applyUserService.selectOne(ew);

        EntityWrapper<InviteCompanyEmp> empEw = new EntityWrapper<>();
        empEw.eq("apply_user_id", applyUser.getId());
        InviteCompanyEmp emp = inviteCompanyEmpService.selectOne(empEw);
        return this.baseMapper.count(emp.getInviteCompanyId(), applyUser.getId());
    }
}
